/**
 * 域名配置表
 */
export class DomainConfig {
    private _config: { [key: number]: string[] } = {
        // 测试
        0: [
            // `10.10.0.74:9030`,
            `10.10.50.92:9030`
        ],
        // 预生产
        1: [
            `156.59.69.38:9030`,
        ],
        // 生产
        2: [
            `puzzle-game.cd.xiaoxigroup.net`,
        ],
    }
    private _curDomain: string = ``

    // ====================================================================================
    // ===================================== 内部接口 ======================================
    // ====================================================================================

    // ====================================== 游戏服 =======================================

    /**
     * 获取当前延迟最低的域名
     * @param serverType 服务器类型
     * @param call 回调
     */
    private _checkFastDomain(serverType: number, call: (domain: string) => void): void {
        cc.log(`DomainConfig _checkFastDomain`)

        let domain = ``,
            delay: number,
            i = 0,
            j = 0,
            ping = (tempDomain) => {
                let startTime = Date.now(),
                    endTime
                cc.assetManager.loadRemote(`https://h5.${tempDomain}/hongxiu/clientResource/project/test.json?time=${Date.now()}`, { timeout: 3 }, (err, data) => {
                    if (err) {
                        cc.error(err)
                    } else {
                        endTime = Date.now()
                        let tempDelay = endTime - startTime
                        if (!delay) {
                            domain = tempDomain
                            delay = tempDelay
                            console.log(`DomainConfig domain = ${domain}`)
                            console.log(`DomainConfig delay = ${delay}`)
                        }
                        if (tempDelay < delay) {
                            domain = tempDomain
                            delay = tempDelay
                            console.log(`DomainConfig domain = ${domain}`)
                            console.log(`DomainConfig delay = ${delay}`)
                        }
                    }
                    if (!delay || delay >= 200) {
                        j++
                        if (this._config[serverType][i][j]) {
                            ping(this._config[serverType][i][j])
                        } else {
                            i++
                            j = 0
                            if (this._config[serverType][i] && this._config[serverType][i][j]) {
                                ping(this._config[serverType][i][j])
                            } else {
                                this._curDomain = domain
                                console.log(`DomainConfig this._curDomain = ${this._curDomain}`)
                                call && call(this._curDomain)
                            }
                        }
                    } else {
                        this._curDomain = domain
                        console.log(`DomainConfig this._curDomain = ${this._curDomain}`)
                        call && call(this._curDomain)
                    }
                })
            }
        if (this._config[serverType][i] && this._config[serverType][i][j]) {
            ping(this._config[serverType][i][j])
        }
    }

    // ====================================================================================
    // ===================================== 外部接口 ======================================
    // ====================================================================================

    // ====================================== 游戏服 =======================================

    /**
     * 检测并获取当前延迟最低的域名
     * @param serverType 服务器类型
     * @param call 回调
     */
    checkFastDomain(serverType: number, call: (domain: string) => void = null): void {
        cc.log(`DomainConfig init`)

        this._curDomain = this._config[serverType][0]
        if (!this._curDomain) {
            this._checkFastDomain(serverType, call)
        } else {
            call && call(this._curDomain)
        }
    }

    /**
     * 设置当前可使用的域名
     * @param domain 域名
     */
    setCurDomain(domain: string): void {
        cc.log(`DomainConfig setCurDomain domain = ${domain}`)

        this._curDomain = domain
    }

    /**
     * 获取当前可使用的域名
     */
    getCurDomain(): string {
        // cc.log(`DomainConfig getCurDomain fast = ${fast}`)

        return this._curDomain
    }

    /**
     * 指定服务器类型是否有配置额外域名
     * @param serverType 服务器类型
     */
    hasDomainsConfig(serverType: number): boolean {
        // cc.log(`DomainConfig hasDomainsConfig serverType = ${serverType}`)

        return this._config[serverType] && this._config[serverType].length > 0
    }
}